<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 08.08.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class adminControlPanel
    ****
    * This class represents the Control Panel Page of any solution
    ****
    * @package adminModules
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 08.08.2005
    * @copyright essentialmind.com 2005
    ***/
    class adminManagenodes extends adminModule{

        /**
         * Process user inputs
         * 
         * @return void 
         **/
        public function process(){
            //now, was there an install call?
            if(isset($_GET['install'])){
                $this->installNode();   
            }
            
            //is there a remove call?
            if(isset($_GET['remove'])){
                $this->removeNode();   
            }
            
            //get the list of availabel nodes
            $availableNodes = $this->getNodes();
            
            //get the list of installed nodes
            $installedNodes = $this->db->getCol("SELECT codeset FROM datatype");
            
            //process a list of nodes, that could be installed
            $toInstall = array_diff($availableNodes,$installedNodes);
            
            /* ----------------------------------------------------------------------- */
            //now get data for smarty, first process
            //the list of all the nodes that may need installation
            //and make sure they feel well
            $nodesToInstall = array();
            foreach($toInstall as $newNode){
                $tName = 'installNode'.ucfirst($newNode);
                $tNode = new $tName($this->db,$this->log);
                
                $element = array();
                $element['name'] = $newNode;
                $element['desc'] = $tNode->getDescription();
                $element['fold'] = $tNode->getIsFolder();
                
                $nodesToInstall[] = $element;
            }
            
            
            //now get the array of already present nodes with some stats
            $myNodes = $this->db->getAll("SELECT id, codeset AS name, name AS desc, folder AS fold FROM datatype WHERE codeset != 'root' ORDER BY id ASC");
            $nodesInstalled = array();
            foreach($myNodes as $node){
                $element = array();
                if($node['fold'] == 't'){
                    $element['total'] = (int)$this->db->getOne("SELECT count(*) FROM structure WHERE node_type = ".$node['id']);
                }else{
                    $element['total'] = (int)$this->db->getOne("SELECT count(*) FROM pagemap WHERE pagetype = ".$node['id']); 
                }
                
                $element['id']   = $node['id'];
                $element['name'] = $node['name'];
                $element['desc'] = $node['desc'];
                $element['fold'] = $node['fold'];
                
                $nodesInstalled[] = $element;
            }
            
            $this->smarty->assign('newnodes',$nodesToInstall);
            $this->smarty->assign('mynodes',$nodesInstalled);
            /* ------------------------------------------------------------------------ */
        }
        
        
        /**
         * Returns a list of available nodes (checking installers)
         *
         * @return unknown
         */
        private function getNodes(){
            $toReturn = array();
            
            //read the install dir
            $d = dir(INA_ROOT.'classes'.DIRECTORY_SEPARATOR.'install');
            while(false !== ($entry = $d->read())){
                if($entry != 'installNode.class.php' and eregi('installNode(.*)\.class\.php$',$entry)){
                    $temp       = str_replace('installNode','',str_replace('.class.php','',$entry));
                    $temp[0]    = strtolower($temp[0]);
                    $toReturn[] = $temp;
                    //$toReturn[] = str_replace('installNode','',str_replace('.class.php','',$entry));
                }
            }
            $d->close();
            
            $d = dir(ROOT.'classes');
            while(false !== ($entry = $d->read())){
                if($entry != 'installNode.class.php' and eregi('installNode(.*)\.class\.php$',$entry)){
                    $temp       = str_replace('installNode','',str_replace('.class.php','',$entry));
                    $temp[0]    = strtolower($temp[0]);
                    $toReturn[] = $temp;
                    //$toReturn[] = str_replace('installNode','',str_replace('.class.php','',$entry));
                }
            }
            $d->close();
                        
            return $toReturn;
        }
        
        
        /**
         * Enter description here...
         *
         */
        private function installNode(){
          
            //bogus check
            if($this->db->getOne("SELECT id FROM datatype WHERE codeset = '".$_GET['install']."'")){
                return;   
            }
          
            //get the name right ;)
            $_GET['install'] = ucfirst($_GET['install']);
            
            //go on
            @include_once 'installNode'.$_GET['install'].'.class.php';

            $tName = 'installNode'.$_GET['install'];
            $tNode = new $tName($this->db,$this->log);
            $newID = $tNode->install();
            
            //redirect
            header("Location: ".$GLOBALS['_CONFIG']['admin_root'].$_GET['view'].'/?added='.$newID);
            exit;
        }
        
        
        /**
         * Enter description here...
         *
         */
        private function removeNode(){
            //bogus check
            if($this->db->getOne("SELECT id FROM datatype WHERE codeset = '".$_GET['install']."'")){
                return;   
            }
            
            //get the name right
            $_GET['remove'] = ucfirst($_GET['remove']);
            
            //go on
            @include_once 'installNode'.$_GET['remove'].'.class.php';
            $tName = 'installNode'.$_GET['remove'];
            $tNode = new $tName($this->db,$this->log);
            $newID = $tNode->uninstall();
            
            //redirect
            header("Location: ".$GLOBALS['_CONFIG']['admin_root'].$_GET['view'].'/?removed='.strtolower($_GET['remove']));
            exit;
        }
        

        /**
         * Set action template
         * 
         * @return void
         **/
        protected function setTemplate(){
            $this->setActionTemplate('managenodes.html');
        }
    }
?>